package com.atguigu.juc.base;

import java.util.concurrent.*;

/**
 * @author yfstart
 * @create 2022-06-11 21:00
 */
public class FutureThreadPoolDemo {
    public static void main(String[] args)
            throws ExecutionException, InterruptedException, TimeoutException {
        // m1();
        // m2();
        m3();
    }

    private static void m3() throws InterruptedException, ExecutionException, TimeoutException {
        FutureTask<String> futureTask = new FutureTask<String>(() -> {
            System.out.println(Thread.currentThread().getName() + "\t ---come in");
            try {
                TimeUnit.MILLISECONDS.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "task3 over";
        });

        Thread t3 = new Thread(futureTask,
                "t3");
        t3.start();

        System.out.println(Thread.currentThread().getName() + "\t ---忙其他任务了");
        // System.out.println(futureTask.get());
        // System.out.println(futureTask.get(2000,TimeUnit.MILLISECONDS));

        while (true) {
            if (futureTask.isDone()) {
                System.out.println(futureTask.get());
                break;
            } else {
                // 暂停
                try {
                    TimeUnit.MILLISECONDS.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("正在处理中----");
            }
        }
    }

    /**
     * 3个任务，目前开启多个线程来处理，请问耗时多久？
     */
    private static void m2() throws ExecutionException, InterruptedException {
        long startTime = System.currentTimeMillis();
        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        FutureTask<String> futureTask1 = new FutureTask<String>(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "task1 over";
        });
        threadPool.submit(futureTask1);

        FutureTask<String> futureTask2 = new FutureTask<String>(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "task2 over";
        });
        threadPool.submit(futureTask2);

        System.out.println("futureTask1.get() = " + futureTask1.get());
        System.out.println("futureTask2.get() = " + futureTask2.get());

        // main 线程
        try {
            TimeUnit.MILLISECONDS.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        threadPool.shutdown();
        long endTime = System.currentTimeMillis();
        System.out.println("---costTime：" + (endTime - startTime) + "毫秒");
    }

    /**
     * 3个任务，目前只有一个线程【main】来处理，请问耗时多久？
     */
    private static void m1() {
        long startTime = System.currentTimeMillis();
        try {
            TimeUnit.MILLISECONDS.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            TimeUnit.MILLISECONDS.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            TimeUnit.MILLISECONDS.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("---costTime：" + (endTime - startTime) + "毫秒");
    }
}
